/*
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.gov.dna.base.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gov.dna.base.mapper.BaseMapper;
import com.gov.dna.base.mybatis.BasePage;
import com.gov.dna.base.mybatis.BaseQuery;
import com.gov.dna.base.service.BaseService;
import com.gov.dna.common.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 扩展ServiceImpl
 * @author (作者) Dong Jie 154046519@qq.com
 * @version V1.0
 * @date 2018/12/5 21:35
 * @since (该版本支持的JDK版本) 1.8
 */
@Slf4j
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {

	@Override
	public boolean logicalDeleteById(T o) {
		try {
			T temp= (T) o.getClass().newInstance();
			ObjectUtil.setValue(temp,"id",ObjectUtil.getFieldValueByName("id",o));
			ObjectUtil.setValue(temp,"deleted",true);
			return retBool(this.baseMapper.updateById(temp));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public T findSingle(T o) {
		List<T> list=find(o);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public BasePage<T> findByPage(T o, BasePage<T> page) {
		page.setRecords(baseMapper.findByPage(o,page));
		return page;
	}

	@Override
	public List<T> find(T o, BasePage<T> page) {
		return baseMapper.find(o,page);
	}

	@Override
	public List<T> find(T o) {
		return baseMapper.find(o,new BaseQuery<T>());
	}
}
